package com.lanju.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lanju.entity.User;
import com.lanju.model.ResultModel;
import com.lanju.model.ResultStatus;
import com.lanju.model.SAModel;
import com.lanju.service.AdminPermissionServiceIf;
import com.lanju.service.JedisService;
import com.lanju.service.OrganizationService;
import com.lanju.service.SystemAdminService;
import com.lanju.service.UserService;
import com.lanju.util.ServletUtil;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Autowired
	SystemAdminService systemAdminService;

	@Autowired
	AdminPermissionServiceIf adminPermissionService;

	@Autowired
	OrganizationService organizationService;

	@Autowired
	UserService userService;
	@Resource
	JedisService jedisService;

	/**
	 * 登录
	 * */
	@RequestMapping("/login")
	@ResponseBody
	public ResultModel login(@RequestParam("adminName") String adminName,
			@RequestParam("password") String password, HttpSession httpSession) {
		ResultModel model = systemAdminService.login(adminName, password);
		if (model.getResultCode() == ResultStatus.RESULT_STATUS_SUCCESS
				.getValue()) {
			SAModel sysModel = (SAModel) model.getContent();
			httpSession.setAttribute("saModel", sysModel);
		}
		return model;

	}
	
	@RequestMapping(value = "/tologinhtml",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView tologinhtml(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
//		response.sendRedirect("../jsp/login.html");
//		ModelAndView mav=new ModelAndView();
//		mav.setViewName("/jsp/login.html");
//		return mav;
		  ModelAndView mv = new ModelAndView("redirect:/jsp/login.html");//redirect模式  
		  return mv;
	}

	/**
	 * 退出
	 * */
	@RequestMapping("/loginout")
	@ResponseBody
	public ResultModel loginOut(HttpSession httpSession) {
		if (httpSession.getAttribute("saModel") != null) {
			httpSession.removeAttribute("saModel");
		}
		return ResultModel.success();
	}

	@RequestMapping("/getAdminPerssionCode")
	@ResponseBody
	public Map<String, Object> getAdminPerssionCode(
			@RequestParam(value = "adminCode") String adminCode) {
		return adminPermissionService.getAdminPerssionCode(adminCode);
	}

	@RequestMapping("/getAllOrganizations")
	@ResponseBody
	public ResultModel getAllOrganizations() {
		return organizationService.getAllOrganizations();

	}

	/**
	 * 获取flag为2的团队管理员
	 * */
	@RequestMapping("/getOrganizeInAdmin")
	@ResponseBody
	public ResultModel getOrganizeInAdmin() {
		return systemAdminService.getOrganizeInAdmin();

	}

	/**
	 * 修改密码
	 * */
	@RequestMapping("/editPwd")
	@ResponseBody
	public ResultModel editPwd(@RequestParam("pwd") String pwd,
			@RequestParam("oldPwd") String oldPwd, HttpServletRequest request,
			HttpSession httpSession) {
		SAModel systemAdminModel = (SAModel) httpSession
				.getAttribute("saModel");
		if (systemAdminModel == null) {
			return ResultModel.fail("会话过期，请重新登录", null);
		}
		return systemAdminService.editPwd(pwd, oldPwd,
				systemAdminModel.getAdminCode());

	}

	/**
	 * 公众号登录
	 * */
	@RequestMapping("/ljLogin")
	@ResponseBody
	public ResultModel ljLogin(HttpServletRequest request,
			HttpServletResponse response,HttpSession session) {
		Map<String, Object> param = ServletUtil.getParameterMap(request);
		 User u =  userService.getByPhone(param);
	        if(u==null){
	            return ResultModel.fail("该手机号码尚未注册", 1);
	        }
	        if(!StringUtils.isEmpty(param.get("flag"))){//管家
	        	if(u.getFlag() !=2){
	        		 return ResultModel.fail("您不是管家，请前往用户端登陆", 1);
	        	}
	        }
	        if(StringUtils.isEmpty(param.get("flag"))){//非管家
	        	if(u.getFlag() ==2){
	        		 return ResultModel.fail("您是管家，请前往管家端登陆", 1);
	        	}
	        }
		User user = userService.getByPhoneAndPassword(param);
		if (user != null) {
			if(user.getStatus()==0){//被拉黑
				return ResultModel.success("账号已被限制登录，请和客服联系", "");
			}else{
				session.setAttribute("userModel", user);
				return ResultModel.success("登陆成功", user);
			}
			
		} else {
			return ResultModel.fail("账号或密码错误", null);
		}
	}

	/**
	 * 公众号退出
	 * */
	@RequestMapping("/ljLogout")
	@ResponseBody
	public ResultModel ljLogout(HttpServletRequest request,
			HttpServletResponse response,HttpSession session) {
		Map<String, Object> param = ServletUtil.getParameterMap(request);
		session.removeAttribute("userModel");
		return ResultModel.success("退出成功", null);
	}

	/**
	 * 微信授权登录
	 * 
	 * @return
	 */
	@RequestMapping("/wechatLogin")
	@ResponseBody
	public void wechatLogin(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			try {
				WeiXinInterfaceController.class.newInstance().Login(request,
						response);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
